package pit.com.ordermobilesystem.a;

import java.util.ArrayList;

import pit.com.ordermobilesystem.R;
import pit.com.ordermobilesystem.http.OrderSystemHttpService;
import pit.com.ordermobilesystem.imageutil.ImageFetcher;
import pit.com.ordermobilesystem.model.Item;
import pit.com.ordermobilesystem.model.OrderItem;
import pit.com.ordermobilesystem.sharepreference.OrderSharePreference;
import pit.com.ordermobilesystem.tabhost.TabGroupActivity;
import pit.com.ordermobilesystem.util.OrderConstant;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class DetailsActivity extends Activity implements OrderConstant {
	
	public Item item;
//	private Item item;
	private static int not = 1;
	private ImageFetcher mImageItem;
	private ImageView imbt_tang;
	private ImageView imbt_giam;
	private ImageView imv_trove;
	private ImageView imv_donhang;
	private ImageView bt_chon;
	
	private TextView tv_soluong;
	private TextView  tv_giashow;
	
	private int tien ;
	private int gia;
	private int giamgia;
	private String url = "";
	// public static Order order;
	public static  ArrayList<OrderItem> ListItem = new ArrayList<OrderItem>();
	

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.detailfragment);
		
		bt_chon = (ImageView) findViewById(R.id.bt_chon);
		imbt_tang = (ImageView) findViewById(R.id.ibt_tang);
		imbt_giam = (ImageView) findViewById(R.id.ibt_giam);
		imv_trove = (ImageView) findViewById(R.id.iv_trove);
		imv_donhang =(ImageView)findViewById(R.id.iv_dathang);
		
		TextView tv_nameitem = (TextView) findViewById(R.id.tv_nameshow);
		TextView tv_description=(TextView)findViewById(R.id.tv_descriptionshow);
		ImageView img_dishitem = (ImageView) findViewById(R.id.img_dishitem);
		TextView tv_vatshow = (TextView) findViewById(R.id.tv_vatshow);
		tv_giashow = (TextView) findViewById(R.id.tv_giashow);
		tv_soluong = (TextView) findViewById(R.id.tv_soluong);
		
			bt_chon.setVisibility(View.VISIBLE);
			imbt_tang.setVisibility(View.VISIBLE);
			imbt_giam.setVisibility(View.VISIBLE);
			imv_trove.setVisibility(View.VISIBLE);

		try {

			Bundle bun = this.getIntent().getExtras();
			item = new Item();
			item.setId(bun.getInt("IdItem"));
			item.setIdCategory(bun.getInt("IdCategory"));
			item.setNameItem(bun.getString("NameItem"));
			item.setPrice(bun.getDouble("Price"));
			item.setDescription(bun.getString("Description"));
			item.setLinkImage(bun.getString("LinkImage"));
			item.setDiscount(bun.getDouble("Discount"));

		} catch (Exception e) {
//			Toast.makeText(this, "du lieu khong co", Toast.LENGTH_LONG).show();
		}
		


		mImageItem = new ImageFetcher(this, 100, 100);
		mImageItem.setLoadingImage(R.drawable.chickencurry);		
		if (!OrderSharePreference.getDownloadSync(this)) {
			mImageItem.setOfflineMode(true);
		}
		
		// wv_dishitem.getSettings().setJavaScriptEnabled(true);
		// String customHtml = "<html><body><img src=" + url
		// + " width=250 height=300/></body></html>";
		// wv_dishitem.loadData(customHtml, "text/html", "UTF-8");
		 gia = (int) item.getPrice();
		 giamgia = (int)item.getDiscount();
		 tien = gia - giamgia;
		 url = OrderSystemHttpService.getImageProductURL(SERVER_HOST, PORT,item.getLinkImage()+".jpg");
	
		 tv_nameitem.setText(item.getNameItem());
			tv_description.setText(item.getDescription());

			mImageItem.loadImage(url, img_dishitem);
			not=1;
			tv_vatshow.setText("Giá trên đã bao gồm 10% VAT và 5% phí phục vụ");
			tv_giashow.setText(not+" x "+String.valueOf(tien)+ ".000 = "+tien*not+ ".000đ");
			tv_soluong.setText(String.valueOf(not));

			imv_trove.setOnClickListener(new View.OnClickListener() {
				
				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					Intent previewMessage = new Intent();
					previewMessage.setClass(DetailsActivity.this, TitlesActivity.class);
		            TabGroupActivity parentActivity = (TabGroupActivity)getParent();
		            parentActivity.startChildActivity("TitlesActivity", previewMessage);
											
				}
			});
			
			imv_donhang.setOnClickListener(new View.OnClickListener() {
				
				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					
						Intent previewMessage = new Intent();
						previewMessage.setClass(DetailsActivity.this, SelectedActivity.class);
			            TabGroupActivity parentActivity = (TabGroupActivity)getParent();
			            parentActivity.startChildActivity("SelectedActivity", previewMessage);
				}
			});
			
			imbt_giam.setOnClickListener(new View.OnClickListener() {

				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					if (not > 1) {
						not = not - 1;
					}
					tv_soluong.setText(String.valueOf(" "+not+" "));
					tv_giashow.setText(not+" x "+String.valueOf(tien)+ ".000 = "+tien*not+ ".000đ");
				}
			});
			
			imbt_tang.setOnClickListener(new View.OnClickListener() {

				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					not = not + 1;
					tv_soluong.setText(String.valueOf(" "+not+" "));
					tv_giashow.setText(not+" x "+String.valueOf(tien)+ ".000 = "+tien*not+ ".000đ");
				}
			});
			
			bt_chon.setOnClickListener(new View.OnClickListener() {
				@Override
				public void onClick(View arg0) {
					// order = new Order();
					// xem xem trong list da co item nay chua
					int tile=0;
					for (int i = 0; i < ListItem.size(); i++) {
						OrderItem neworderitem = new OrderItem();
						neworderitem = ListItem.get(i);
						if (neworderitem.getIdItem() == item.getId()) {
							
							tile=1;
						}
						
					}
					if(tile==1){
						Toast.makeText(DetailsActivity.this, 
		                        "Món ăn đã có trong đơn hàng", Toast.LENGTH_LONG).show();
					}else{
						OrderItem orderitem = new OrderItem();
						orderitem.setIdItem(item.getId());
						orderitem.setNumberItem(not);
						orderitem.setPrice(item.getPrice());
						orderitem.setNameItem(item.getNameItem());
						orderitem.setDiscount(item.getDiscount());
						// getActivity().getContentResolver().insert(OrderMobileProvider.ORDER_ITEM_CONTENT_URI,
						// OrderMobileProvider.getContentValuesOrder_Item(orderitem));
						// them vao khi ma no chua co va so phai >=1

						ListItem.add(orderitem);
						Toast.makeText(DetailsActivity.this, 
		                        "Món ăn đã được thêm vào đơn hàng", Toast.LENGTH_LONG).show();
					}
					
					
					not = 1;				

				}
			});
	}

}
